package com.webserver;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.Socket;

public class Processor extends Thread {
    private Socket socket;
    private InputStream in;
    private PrintStream out;
    private final static String WEB_ROOT="F:\\projects\\projects\\webserver\\htdocs";
    
    /**
     * 根据socket对象，获得输入流。获得输出流对象，包装为输出流打印对象。构造处理器对象。
     * @param socket
     */
    public Processor(Socket socket) {
        this.socket = socket;
        try {
            in = socket.getInputStream();
            out = new PrintStream(socket.getOutputStream());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    
    /**
     * 线程启动，解析输入流对象，返回文件
     */
    public void run() {
        String filename = parse(in);
        sendFile(filename);
    }
    
    /**
     * 把输入流对象，包装为输入字节码读取对象。
     * 读取第一行，分割为字符数组。打印代码，文件名，http版本号。
     * 返回文件名
     * @param in
     * @return
     */
    public String parse(InputStream in) {
        BufferedReader br = new BufferedReader(new InputStreamReader(in));
        String filename = null;
        try {
            String httpMessage = br.readLine();
            String[] content = httpMessage.split(" ");
            
            if(content.length!=3){
                sendErrorMessage(400, "Client query error!");
                return null;
            }
            
            System.out.println("code:"+content[0]+",filename" + content[1] + ",http version:" + content[2]);
            filename = content[1];
        } catch (IOException e) {
            e.printStackTrace();
        }
        
        return filename;
    }

    /**
     * 通过打印输出流对象，打印错误代码和错误信息
     * @param errorCode
     * @param errorMessage
     */
    public void sendErrorMessage(int errorCode,String errorMessage){
        out.println("HTTP/1.0 " + errorCode+" " + errorMessage);
        out.println("content-type: text/html");
        out.println();
        out.println("<html>");
        out.println("<title>Error Message");
        out.println("</title>");
        out.println("<body>");
        out.println("<h1>ErrorCode:" + errorCode + ", Message:" + errorMessage + "</h1>");
        out.println("</body>");
        out.println("</html>");
        out.close();
        try {
            in.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    
    /**
     * 根据文件名，读取本地文件，包装为文件输入流。
     * 将文件输入流读入字节数组
     * 通过打印输出流对象，把字节数组输出打印返回到浏览器，
     * @param filename
     */
    public void sendFile(String filename){
        File file = new File(Processor.WEB_ROOT+filename);
        if(!file.exists()){
            sendErrorMessage(404,"File Not Found!");
            return;
        }
        try {
            InputStream in = new FileInputStream(file);
            byte content[] = new byte[(int)file.length()];
            in.read(content);
            out.println("HTTP/1.0 200 queryfile");
            out.println("content-length:" + content.length);
            out.println();
            out.write(content);
            out.close();
            in.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
